#include "Term.h"
#include "CategoryBlock.h"
#include <QXmlStreamWriter>

Term::~Term()
{
	QHashIterator<QString, CategoryBlock*> it(_categories);
	while (it.hasNext()) {
		it.next();
		delete it.value();
	}
}

void Term::dump(QXmlStreamWriter& writer)
{
	bool entriesWritten = false;

	if (_categories.contains("PrincipalTranslations")) {
		_categories.value("PrincipalTranslations")->dump(writer);
	} else if (_categories.contains("Entries")) {
		entriesWritten = true;
		_categories.value("Entries")->dump(writer);
	}

	if (_categories.contains("AdditionalTranslations")) {
		// Add horizontal line separator
		writer.writeStartElement("div");
		writer.writeAttribute("class", "hr");
		writer.writeTextElement("hr", "");
		writer.writeEndElement(); // div
		_categories.value("AdditionalTranslations")->dump(writer);
	}

	if (_categories.contains("Entries") && !entriesWritten) {
		// Add horizontal line separator
		writer.writeStartElement("div");
		writer.writeAttribute("class", "hr");
		writer.writeTextElement("hr", "");
		writer.writeEndElement(); // div
		_categories.value("Entries")->dump(writer);
	}

	if (_categories.contains("OtherSideEntires")) {
		// Add horizontal line separator
		writer.writeStartElement("div");
		writer.writeAttribute("class", "hr");
		writer.writeTextElement("hr", "");
		writer.writeEndElement(); // div
		_categories.value("OtherSideEntires")->dump(writer);
	}
}

